package com.emns.service.zhihui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.emns.dao.LanmuMapper;
import com.emns.dao.LanmuZiyuanMapper;
import com.emns.dao.ZhihuiMapper;
import com.emns.pojo.Lanmu;
import com.emns.pojo.LanmuZiyuan;
import com.emns.pojo.ShiJie;
import com.emns.pojo.Zhihui;
import com.emns.service.zhihui.entity.ZhihuiMobileMess;
import com.emns.web.zhihui.LanmuEntity;

import common.PageInfo;

@Service
public class ZhihuiService {
	@Autowired
	ZhihuiMapper zm;
	@Autowired
	LanmuMapper lmm;
	@Autowired
	LanmuZiyuanMapper lzym;

	/**
	 * 添加智慧
	 * 
	 * @param zhInfo
	 * @return
	 */
	public int addZhihui(Zhihui zhInfo) {
		return zm.insert(zhInfo);
	}

	/**
	 * 默认查询
	 * 
	 * @param nowPage
	 * @return
	 */
	public PageInfo<Zhihui> queryAll(int nowPage) {
		// PageInfo<Zhihui> page = new PageInfo<Zhihui>();
		// page.setPageNum(nowPage);
		// int count =zm.selectAllCount();
		// page.setPageCount(count);
		// int index = (nowPage - 1) * page.getPageSize();
		// List<Zhihui> dataArray =zm.selectAll(index, page.getPageSize());
		// page.setDataArray(dataArray);
		//
		// return page;
		return null;
	}

	/**
	 * 根据ID删除
	 * 
	 * @param zid
	 * @return
	 */
	public int dropZhihuiById(Integer[] zid) {
		int result = 0;
		for (Integer id : zid) {
			if (id != null) {
				result += zm.dropZhihui(id);
			}
		}
		return result;
	}

	public PageInfo<Zhihui> queryByPram(int nowPage, String zName,
			String uName, Date bDate, Date eDate) {

		zName = "%" + zName + "%";
		uName = "%" + uName + "%";
		PageInfo<Zhihui> page = new PageInfo<Zhihui>();
		page.setPageNum(nowPage);
		int count = zm.selectPramCount(uName, zName, bDate, eDate);
		page.setPageCount(count);
		int index = (nowPage - 1) * page.getPageSize();
		List<Zhihui> dataArray = zm.selectPram(uName, zName, bDate, eDate,
				index, page.getPageSize());
		page.setDataArray(dataArray);

		return page;
		// return null;

	}

	/**
	 * 查询栏目
	 * 
	 * @return
	 */
	public LanmuEntity getMulu() {
		LanmuEntity lanmuArray = new LanmuEntity();
		lanmuArray.setFirstLanmu(lmm.selectFrist());
		lanmuArray.setSecondList(lmm.selectSecond());
		return lanmuArray;
	}

	public List<Lanmu> getSLanmu() {
		return lmm.selectSecond();
	}

	/**
	 * 根据主键查询栏目
	 * 
	 * @param id
	 * @return
	 */
	public Lanmu getLanmuById(int id) {
		return lmm.selectByPrimaryKey(id);
	}

	/**
	 * 添加资源栏目映射
	 * 
	 * @param lmzy
	 * @return
	 */
	public int addLanmuZiyuan(LanmuZiyuan lmzy) {
		return lzym.insertSelective(lmzy);
	}

	/**
	 * 查询栏目资源映射
	 * 
	 * @param zid
	 * @return
	 */
	public List<LanmuZiyuan> getLanmuZiyuan(int zid) {
		return lzym.selectByZid(zid);
	}

	/**
	 * 查询智慧
	 * 
	 * @param zid
	 * @return
	 */
	public Zhihui getZhihuiById(int zid) {
		return zm.selectByPrimaryKey(zid);
	}

	/*
	 * 删除资源
	 * 
	 * @param zid
	 * 
	 * @return
	 */
	public int deleteZiyuan(int zid) {
		return lzym.deleteZylmByZid(zid);

	}

	/**
	 * 修改资源
	 * 
	 * @param zhihui
	 * @return
	 */
	public int updateZhihui(Zhihui zhihui) {
		return zm.updateByPrimaryKeySelective(zhihui);
	}

	// /////////////////////////////////
	// mobile service
	// ///////////////////////////////
/**
 * 查询列表
 * @param nowPage
 * @param flm
 * @param slm
 * @return
 */
	public PageInfo queryZhihuiList(int nowPage, int flm, int slm) {

		PageInfo<ZhihuiMobileMess> page = new PageInfo<ZhihuiMobileMess>();
		page.setPageNum(nowPage);
		int count = zm.queryListCountByMobile(flm, slm);
		page.setPageCount(count);
		int index = (nowPage - 1) * page.getPageSize();
		List<ZhihuiMobileMess> dataArray = zm.queryListByMobile(flm, slm,
				index, page.getPageSize());
		page.setDataArray(dataArray);

		return page;
	}
	/**
	 * 搜索
	 * @param nowPage
	 * @param str
	 * @return
	 */
	public PageInfo searchZhihuiList(int nowPage, String str) {
		str = "%" + str + "%";
		PageInfo<ZhihuiMobileMess> page = new PageInfo<ZhihuiMobileMess>();
		page.setPageNum(nowPage);
		int count = zm.searchListCountByMobile(str);
		page.setPageCount(count);
		int index = (nowPage - 1) * page.getPageSize();
		List<ZhihuiMobileMess> dataArray = zm.searchListByMobile(str,index, page.getPageSize());
		page.setDataArray(dataArray);

		return page;
	}
}
